package com.dosen.api.thought.core.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dosen.api.base.common.asserts.AssertNP;
import com.dosen.api.common.result.page.PageUtils;
import com.dosen.api.common.result.page.Query;
import com.dosen.api.thought.core.service.dao.ConsciousnessThoughtRefDao;
import com.dosen.api.thought.core.service.entity.ConsciousnessThoughtRefEntity;
import com.dosen.api.thought.core.service.service.ConsciousnessThoughtRefService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Map;


@Service("consciousnessThoughtRefService")
public class ConsciousnessThoughtRefServiceImpl extends ServiceImpl<ConsciousnessThoughtRefDao, ConsciousnessThoughtRefEntity> implements ConsciousnessThoughtRefService {
    private static final Logger log = LoggerFactory.getLogger(ConsciousnessThoughtRefServiceImpl.class);




    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        if (!AssertNP.legalParams(params)) {
            params.put("page", 1);
            params.put("limit", 10);
            params.put("key", "cid=1&tid=1");
            log.info("请求参数为空,启用默认查询");
        }
        String key = (String) params.get("key");
        String cid = "";
        String tid = "";
        QueryWrapper<ConsciousnessThoughtRefEntity> wrapper = null;
        if (AssertNP.legalParams(key)) {
            boolean and = false;
            String[] KeySplit = null;
            if (key.contains("&")) {
                and = true;
                KeySplit = key.split("&");
            } else if (key.contains("||")) {
                KeySplit = key.split("\\|\\|");
            } else if (key.contains("|")) {
                KeySplit = key.split("\\|");
            }
            if (AssertNP.okArray(KeySplit)) {
                cid = KeySplit[0].split("=")[1].trim();
                tid = KeySplit[1].split("=")[1].trim();
            }
            if (and) {
                String finalTid = tid;
                wrapper = new QueryWrapper<ConsciousnessThoughtRefEntity>().eq("consciousness_id", cid).and(item -> item.eq("thought_id", finalTid));
            } else {
                wrapper = new QueryWrapper<ConsciousnessThoughtRefEntity>().eq("consciousness_id", cid).or().eq("thought_id", tid);
            }
        }
        IPage<ConsciousnessThoughtRefEntity> page = this.page(new Query<ConsciousnessThoughtRefEntity>().getPage(params), wrapper);
        return new PageUtils(page);
    }

}
